package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkFalseClueDto;
import com.qk.entity.Clue;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;
    /**
     * 新增线索
     */
    @PostMapping
    public Result add(@RequestBody Clue clue) {
        log.info("新增线索{}",clue);
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save( clue);
        return Result.success();
    }
    /**
     * 线索列表查询
     */
    @GetMapping
    public Result list(ClueDto clueDto) {
        log.info("查询线索{}",clueDto);
        PageResult<Clue> pageResult = clueService.querylist(clueDto);
        return Result.success(pageResult);

    }
    /**
     * 线索跟进，修改数据
     */
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId,@PathVariable Integer userId) {
        log.info("线索{}跟进{}",clueId,userId);
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setStatus(2);
        clue.setUserId(userId);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }
    /**
     * 根据ID 回显线索信息和跟进记录
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("回显线索信息{}",id);
        Clue clue = clueService.queryById(id);
        return Result.success(clue);
    }
    /***
     * 修改线索跟踪信息
     */
    @PutMapping
    public Result update(@RequestBody Clue clue) {
        log.info("修改线索{}",clue);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateClue(clue);
        return Result.success();
    }
    /**
     * 虚伪线索的标记
     */
    @PutMapping("/false/{id}")
    public Result update(@PathVariable Integer id, @RequestBody MarkFalseClueDto markFalseClueDto) {
        log.info("修改线索{}",id);
        clueService.updateFalseClue(id,markFalseClueDto);
        return Result.success();
    }
    /**
     * 线索转商机
     */
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("线索转商机{}",id);
        clueService.toBusiness(id);
        return Result.success();
    }
    /**
     * 线索池
     */
    @GetMapping("/pool")
    public Result pool(ClueDto clueDto) {
        log.info("线索池{}",clueDto);
        PageResult<Clue> pageResult = clueService.querylistPool(clueDto);
        return Result.success(pageResult);
    }


}
